<?php

use Biz\model\wiki\WikiRankingConfig;
use Biz\service\wikiRanking\Ranking;
use Doraemon\tools\ActionLog;

/**
 * 榜单详情
 */
class DetailAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $type = $this->getRequest()->getQuery('type', 1);//榜单类型 1：热度榜 2：口碑榜 3：品牌榜
        $parentId = $this->getRequest()->getQuery('parent_id', null);//一级榜单id（默认0：热门） null 根据 二级id查询对应1级id
        $rankingId = $this->getRequest()->getQuery('ranking_id', 0);//榜单id（即二级id）
        if ($rankingId == 0) {
            $rankingId = Ranking::getDefaultRankingId();
        }
        $rankingInfo = WikiRankingConfig::getByWhere([
            'deep' => 2,
            'id' => $rankingId,
            'config_state' => WikiRankingConfig::CONFIG_STATE_SHOW,
        ], '*');
        $result = [];
        if ($rankingInfo) {
            if (is_null($parentId)) {
                $pid = $rankingInfo['pid'];
            } else {
                $pid = $parentId;
            }
            $list = [];
            switch ($type) {
                case Ranking::TYPE_HOT:
                    $list = Ranking::getHotByRankingId($rankingId, $this->uid);
                    break;
                case Ranking::TYPE_PUBLIC_PRAISE:
                    $list = Ranking::getUserLikeByRankingId($rankingId, $this->uid);
                    break;
                case Ranking::TYPE_BRAND:
                    $list = Ranking::getBrandByRankingId($rankingId, $this->uid);
                    break;
            }
            $result = [
                'info' => [
                    'type' => $type,
                    'pid' => $pid,
                    'id' => $rankingInfo['id'],
                    'ranking_name' => $rankingInfo['ranking_name'],
                ],
                'list' => $list
            ];
        }
        $this->actionLog($type, $rankingId);
        $this->setSuccess($result);
    }

    private function actionLog($type, $rankingId)
    {
        switch ($type) {
            case Ranking::TYPE_HOT:
                ActionLog::log($this->uid, ['ranking_id' => $rankingId], ActionLog::WIKI_RANKING_HOT);
                break;
            case Ranking::TYPE_PUBLIC_PRAISE:
                ActionLog::log($this->uid, ['ranking_id' => $rankingId], ActionLog::WIKI_RANKING_PUBLIC_PRAISE);
                break;
            case Ranking::TYPE_BRAND:
                ActionLog::log($this->uid, ['ranking_id' => $rankingId], ActionLog::WIKI_RANKING_BRAND);
                break;
        }

    }
}